package org.ccay.core.ioc;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;

/**
 * 代理类，用来代理一个AnnotationMetadata，以便屏蔽Spring的API
 * 
 * Created：2011-9-15
 * @author chaowangbang
 * @version:
 */
public class AnnotationMetadataDelegate {

	/** Annotation的全限定名 **/
	private String annotationType;

	/** 对应的Spring AnnotationMetadata **/
	private AnnotationMetadata annotationMetadata;

	/**带anotation所在的类的类名 **/
	public static final String CONTAIN_ANNOTATION_CLASS_NAME = "containAnnotationClassName";
	
	/**
	 * 构造函数
	 * 
	 * @param annotationType
	 * @param annotationMetadata
	 */
	public AnnotationMetadataDelegate(String annotationType, AnnotationMetadata annotationMetadata) {
		assert(annotationMetadata!=null);
		
		this.annotationType = annotationType;
		this.annotationMetadata = annotationMetadata;
	}

	/**
	 * 获取Annotation的属性列表
	 * 
	 * @return
	 */
	public Map<String, Object> getAnnotationAttributes() {
		Map<String, Object> attributesMap = annotationMetadata.getAnnotationAttributes(this.annotationType);
		annotationMetadata.getAnnotationAttributes("org.ccay.core.annotation.CcayEmptyOperations");
		attributesMap.put(CONTAIN_ANNOTATION_CLASS_NAME, annotationMetadata.getClassName());
		return attributesMap;
	}

	/**
	 * 获取指定的Annotation的属性列表
	 * 
	 * @return
	 */
	public Map<String, Object> getAnnotationAttributes(String annotation) {
		Map<String, Object> attributesMap = annotationMetadata.getAnnotationAttributes(annotation);
		if(null == attributesMap){
			return null;
		}
		attributesMap.put(CONTAIN_ANNOTATION_CLASS_NAME, annotationMetadata.getClassName());
		return attributesMap;
	}
	
	/**
	 * 遍历方法列表，获取标识了指定Annotation的方法列表
	 * 
	 * @param annotationType
	 * @return
	 */
	public Set<MethodMetadataDelegate> getAnnotatedMethods(String annotationType) {
		Set<MethodMetadataDelegate> anotatedMethodDelegates = new HashSet<MethodMetadataDelegate>();

		Set<MethodMetadata> annotatedMethods = this.annotationMetadata.getAnnotatedMethods(annotationType);
		for (MethodMetadata methodMetadata : annotatedMethods) {
			anotatedMethodDelegates.add(new MethodMetadataDelegate(methodMetadata));
		}

		return anotatedMethodDelegates;
	}

}
